package util;

import java.io.File;

public class StringUtil {
	/**
	 * Check if the string is empty.
	 * 
	 * @param str
	 * @return boolean
	 */
	public static boolean isEmpty(String str) {
		boolean isEmpty = false;
		if (str == null || str.trim().equals("")) {
			isEmpty = true;
		}
		return isEmpty;
	}

	/**
	 * convert string to upper case.
	 * 
	 * @param str
	 * @return String
	 */
	public static String toUpperCase(String str) {
		String outStr = null;
		if (!isEmpty(str)) {
			outStr = str.toUpperCase();
		}
		return outStr;
	}
	/**
	 * Check if the string is empty.
	 * 
	 * @param str
	 * @return String
	 */
	public static String trim(String str) {
		String returnValue = null;
		if (!isEmpty(str)) {
			returnValue = str.trim();
		}
		return returnValue;
	}

	/**
	 * Check if all characters are digits.
	 * 
	 * @param str
	 * @return boolean
	 */
	public static boolean isNumerical(String str) {
		boolean isNumerical = true;
		if (!isEmpty(str)) {
			int i = 0;
			// ensure all characters are digits
			while (i < str.length()) {
				if (!Character.isDigit(str.charAt(i))) {
					isNumerical = false;
					break;
				}
				i++;
			}
		}
		return isNumerical;
	}
	
	/**
	 * convert string to Integer.
	 * 
	 * @param str
	 * @return Integer
	 */
	public static Integer toInteger(String str) {
		Integer outInteger = null;
		if (!isEmpty(str)) {
			outInteger = Integer.valueOf(str);
		}
		return outInteger;
	}
	
	public static void createDir(String path) {
		File dir = new File(path);
		String[] filesIn = dir.list();
		if (filesIn == null) {
			dir.mkdir();
		}
	}
	
	public static int filesInDir(String path) 
	{
		File dir = new File(path);

		String[] children = dir.list();
		if (children == null) {			
		    // Either dir does not exist or is not a directory
			// create directory and ancestor, if not exist
			if ( (new File(path)).mkdir() )
			{
//				System.out.println(path + " is created !");
			}
			
			return 0;
		} else {
//		    System.out.println("# of files: " + children.length);
		    return children.length;
		}	
	}
}
